pub fn use_collections(){
    use_vec();
    use_string();
    use_hash_map();
}

fn use_vec(){
    { //create
        let _vector: Vec<i32> = Vec::new();
        let _vector = vec![1, 2, 3, 4];
    }
    { // mutable  push 
        let mut v = vec![2, 4, 6, 8];
        v.push(10);
        v.push(12);
        println!("{:?}", v);
    }
    { // mutable append
        let mut v = vec![1, 3, 5, 7];
        let mut m_v = vec![2, 4, 6, 8];
        println!("mutable_vec is:{:?}", m_v);
        v.append(&mut m_v);
        println!("after append to v, mutable_vec is:{:?}", m_v);
    }
    { //get 
        let v = vec![1, 2, 4, 8];
        if let Some(value) = v.get(0) {
            println!("{}", value.to_string());
        } else {
            println!("None");
        }
        println!("{}", v[1]);
    }
    { //immutable iter
        let v = vec![100, 101, 102, 103, 104];
        for i in &v {
            println!("{}", i);
        }
    }
    { //mutable iter
        let mut v = vec![ 201, 202, 203, 204];
        for i in &mut v{
            *i += 50;
        }
        println!("{:?}", v);
    }
}
fn use_string(){
    { // 
        let _string = String::new();
        let _one = 1.to_string();
        let _float = 1.3.to_string();
        let _slice = "slice".to_string();
    }
    {
        let hello = String::from("我们");
        println!("len={}, char={}", hello.len(), hello.chars().count());
    }
    { //mutable 
        let mut s = String::from("hello");
        s.push_str(" world");
        s.push('!');
        println!("s:{}", s);

        let s1 = String::from("+new string");
        let s_all = s + "+&str" + &s1;
        println!("s_all:{}", s_all);
    }
    { //format!
        let s1 = String::from("hello");
        let s2 = String::from(" ");
        let s3 = String::from("world");
        let _s = format!("{}-{}-{}", s1, s2, s3);

    }
    {// iter
        let s = String::from("Hello");
        for c in s.chars(){
            println!("{}", c);
        }
    }
    {// get 
        let s = String::from("中国");
        let a = s.chars().nth(1);
        println!("{:?}", a);
    }
    {// substr
        let s = String::from("中国");
        let sub = &s[..3]; // sub[byte-index];
        println!("sub={}", sub);
    }
}

use std::collections::HashMap;
fn use_hash_map(){
    let mut hash = HashMap::new();

    hash.insert("color", "red");
    hash.insert("size", "10 m^2");

    for p in hash.iter(){
        println!("{:?}", p); //p is (&&str, &&str)
    }

    {//
        hash.entry("color").or_insert("red1");// if entry("color") not exist => insert color:red1 ; if exist ,do nothing
        for p in hash.iter(){
            println!("{:?}", p); //p is (&&str, &&str)
        }
    }
    {
        let mut map = HashMap::new();
        map.insert(1, "a");

        if let Some(x) = map.get_mut(&1){
            *x = "b"; // exist -> replace it 
        } else {
            map.insert(1, "a"); // not exist ->insert new one
        }
        println!("{:?}", map);
    }
}
